package com.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;

/**
 * Author Name: Justice
 * Project Name: zookeepercurator
 * File Name: CuratorBase
 * Package Name: com.curator
 * Date: 2020-02-22 20:15
 * Copyright (c) 2020,All Rights Reserved.
 * Curator-API
 */


public class CuratorBase {
    public static void main(String[] args) throws Exception {
        // 重试策略: 初试时间为10s, 重试5次
        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 5);
        // 通过工厂创建连接
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(AddressUtil.ADDRESS)
                .connectionTimeoutMs(10000).sessionTimeoutMs(10000)
                .retryPolicy(retry)
                .build();
        // 开启连接
        curatorFramework.start();

//        创建结点数据, curator中可以递归创建结点, 会自动在父节点不存在的情况下帮我们创建
          curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(
                  "/curatoronline03", "刚刚创建".getBytes());
//          curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/a/b/c/d/e/f/g", "字母哥".getBytes());

//        获取结点数据
//        byte[] bytes = curatorFramework.getData().forPath("/curatoronline03");
//        System.err.println(new String(bytes));

//        更新结点数据
//        curatorFramework.setData().forPath("/a", "居然是版本的问题".getBytes());
//        byte[] bytes = curatorFramework.getData().forPath("/curatoronline03");
//        System.err.println(new String(bytes));

//        删除结点
//        curatorFramework.delete().forPath("/curatoronline03");

//        获取子节点注意只能获取到自己的子节点, 不包含孙节点
//        List<String> list = curatorFramework.getChildren().forPath("/a");
//        System.err.println(list);

//        递归删除
//        curatorFramework.delete().deletingChildrenIfNeeded().forPath("/a");

//        异步执行, 创建结点
//        curatorFramework.create().withMode(CreateMode.PERSISTENT).inBackground(new BackgroundCallback() {
//            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
//                System.err.println("创建成功");
//            }
//        }).forPath("/a", "hello".getBytes());

//        监听
//        NodeCache nodeCache = new NodeCache(curatorFramework, "/a", true);
//        nodeCache.getListenable().addListener(new NodeCacheListener() {
//            public void nodeChanged() throws Exception {
//                System.err.println("结点发生改变");
//            }
//        });
//        curatorFramework.setData().forPath("/a", "sb".getBytes());

//        true代表除了监听子节点的增删改查之外还要监听子节点的内容变化
//        PathChildrenCache pathChildrenCache=new PathChildrenCache(curatorFramework,"/a",true);
//        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
//            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
//                // 事件类型
//                PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
//                switch (type) {
//                    case CHILD_ADDED:
//                        break;
//                    case CHILD_REMOVED:
//                        break;
//                    case CHILD_UPDATED:
//                        break;
//                }
//            }
//        });
        System.err.println("执行完成");
        System.in.read();
    }
}
